package com.yic.module.wms.controller.admin.erpproducein;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.base.api.item.ItemApi;
import com.yic.module.base.api.item.dto.ItemRespDTO;
import com.yic.module.base.api.workshop.WorkshopApi;
import com.yic.module.base.api.workshop.dto.WorkshopRespDTO;
import com.yic.module.mes.api.plan.PlanApi;
import com.yic.module.mes.api.plan.dto.PlanRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.wms.controller.admin.erpproducein.vo.*;
import com.yic.module.wms.dal.dataobject.erpproducein.ErpProduceInDO;
import com.yic.module.wms.convert.erpproducein.ErpProduceInConvert;
import com.yic.module.wms.service.erpproducein.ErpProduceInService;

@Tag(name = "管理后台 - erp生产入库")
@RestController
@RequestMapping("/wms/erp-produce-in")
@Validated
public class ErpProduceInController {

    @Resource
    private ErpProduceInService erpProduceInService;
    @Resource
    private FactoryApi factoryApi;
    @Resource
    private WorkshopApi workshopApi;
    @Resource
    private PlanApi planApi;
    @Resource
    private ItemApi itemApi;

    @PostMapping("/create")
    @Operation(summary = "创建erp生产入库")
    @PreAuthorize("@ss.hasPermission('wms:erp-produce-in:create')")
    public CommonResult<Long> createErpProduceIn(@Valid @RequestBody ErpProduceInCreateReqVO createReqVO) {
        return success(erpProduceInService.createErpProduceIn(createReqVO));
    }

    @DeleteMapping("/cancel")
    @Operation(summary = "撤销erp生产入库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:erp-produce-in:cancel')")
    public CommonResult<Boolean> cancelErpProduceIn(@RequestParam("id") Long id) {
        erpProduceInService.cancelErpProduceIn(id);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新erp生产入库")
    @PreAuthorize("@ss.hasPermission('wms:erp-produce-in:update')")
    public CommonResult<Boolean> updateErpProduceIn(@Valid @RequestBody ErpProduceInUpdateReqVO updateReqVO) {
        erpProduceInService.updateErpProduceIn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除erp生产入库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:erp-produce-in:delete')")
    public CommonResult<Boolean> deleteErpProduceIn(@RequestParam("id") Long id) {
        erpProduceInService.deleteErpProduceIn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得erp生产入库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:erp-produce-in:query')")
    public CommonResult<ErpProduceInRespVO> getErpProduceIn(@RequestParam("id") Long id) {
        ErpProduceInDO erpProduceIn = erpProduceInService.getErpProduceIn(id);
        ErpProduceInRespVO respVO = ErpProduceInConvert.INSTANCE.convert(erpProduceIn);
        PlanRespDTO plan = planApi.getPlan(respVO.getPlanId());
        if (ObjectUtil.isNotEmpty(plan)) {
            respVO.setPlanCode(plan.getCode());
        }
        ItemRespDTO item = itemApi.getItem(respVO.getItemId());
        if (ObjectUtil.isNotEmpty(item)) {
            respVO.setItemName(item.getName());
            respVO.setItemCode(item.getCode());
            respVO.setItemSpec(item.getSpec());
            respVO.setDrawCode(item.getDrawCode());
        }
        return success(respVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得erp生产入库列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('wms:erp-produce-in:query')")
    public CommonResult<List<ErpProduceInRespVO>> getErpProduceInList(@RequestParam("ids") Collection<Long> ids) {
        List<ErpProduceInDO> list = erpProduceInService.getErpProduceInList(ids);
        return success(ErpProduceInConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得erp生产入库分页")
    @PreAuthorize("@ss.hasPermission('wms:erp-produce-in:query')")
    public CommonResult<PageResult<ErpProduceInRespVO>> getErpProduceInPage(@Valid ErpProduceInPageReqVO pageVO) {
        PageResult<ErpProduceInDO> pageResult = erpProduceInService.getErpProduceInPage(pageVO);
        PageResult<ErpProduceInRespVO> respVo = ErpProduceInConvert.INSTANCE.convertPage(pageResult);
        respVo.getList().forEach(resp -> {
            FactoryRespDTO factory = factoryApi.getFactory(resp.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                resp.setFactoryName(factory.getName());
            }
            WorkshopRespDTO workshop = workshopApi.getWorkshop(resp.getWorkshopId());
            if (ObjectUtil.isNotEmpty(workshop)) {
                resp.setWorkshopName(workshop.getName());
            }
            PlanRespDTO plan = planApi.getPlan(resp.getPlanId());
            if (ObjectUtil.isNotEmpty(plan)) {
                resp.setPlanCode(plan.getCode());
            }
            ItemRespDTO item = itemApi.getItem(resp.getItemId());
            if (ObjectUtil.isNotEmpty(item)) {
                resp.setItemName(item.getName());
                resp.setItemCode(item.getCode());
                resp.setItemSpec(item.getSpec());
                resp.setDrawCode(item.getDrawCode());
            }
        });
        return success(respVo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出erp生产入库 Excel")
    @PreAuthorize("@ss.hasPermission('wms:erp-produce-in:export')")
    @OperateLog(type = EXPORT)
    public void exportErpProduceInExcel(@Valid ErpProduceInExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ErpProduceInDO> list = erpProduceInService.getErpProduceInList(exportReqVO);
        // 导出 Excel
        List<ErpProduceInExcelVO> datas = ErpProduceInConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "erp生产入库.xls", "数据", ErpProduceInExcelVO.class, datas);
    }

}
